package com.iweb.service;

import com.common.datasource.DataSourceUtil;
import com.iweb.dao.CommunityDao;
import com.iweb.dao.CommunityDaoImpl;
import com.iweb.pojo.Community;

import java.util.ArrayList;
import java.util.List;

/*
 *作者：lth
 *时间：2020/8/12 18:36
 *描述：
 */
public class CommunityService {

    public List<Community> findAll(){
        List<Community> communities=new ArrayList<>();
        //连接数据库
        DataSourceUtil dataSourceUtil=new DataSourceUtil();
        dataSourceUtil.init();
        //创建数据库操作对象
        CommunityDao communityDao=new CommunityDaoImpl(dataSourceUtil.getConnection());

        try {
            //业务逻辑
            communities=communityDao.findAll();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            dataSourceUtil.close();
        }
        return communities;
    }

    public Community findByUsername(String username) {
        Community community=null;
        //连接数据库
        DataSourceUtil dataSourceUtil=new DataSourceUtil();
        dataSourceUtil.init();
        //创建数据库操作对象
        CommunityDao communityDao=new CommunityDaoImpl(dataSourceUtil.getConnection());

        try{
            community=communityDao.selectByUsername(username);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            dataSourceUtil.close();
        }
        return community;
    }

    public Integer updateByName(String username,Integer type) {
        //连接数据库
        DataSourceUtil dataSourceUtil=new DataSourceUtil();
        dataSourceUtil.init();
        //创建数据库操作对象
        CommunityDao communityDao=new CommunityDaoImpl(dataSourceUtil.getConnection());

        try{
            //更新点赞数
            type=communityDao.updateByName(username,type);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            dataSourceUtil.close();
        }
        return type;
    }
}
